define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.username', title: __('用户名')},
                        {field: 'patient.name', title: __('就诊人')},
                        {field: 'worker_id', title: __('陪诊员的用户id')},
                        {field: 'worker.username', title: __('陪诊员')},
                        {field: 'hospital_id', title: __('Hospital_id')},
                        {field: 'hospital.name', title: __('医院名')},
                        {field: 'department.name', title: __('科室名')},
                        {field: 'servicebasic.name', title: __('Servicebasic.name')},
                        {field: 'order_sn', title: __('订单编号')},
                        {field: 'coupon_money', title: __('Coupon_money'), operate:'BETWEEN'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {
                                '1':'待支付','2':'已支付','3':'已匹配（待接单）','4':'已接单','5':'服务中','6':'已完成','7':'双方已评',
                                '10':'已取消','11':'支付超时自动取消','12':'失效加时单','15':'陪诊员取消订单申请中',
                                '41':'退款审核中','42':'退款成功','43':'退款拒绝'
                            }
                        },
                        {field: 'begin_time', title: __('begin_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'finish_time', title: __('Finish_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recall: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    recall_url: 'order/recall' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.recall_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.username', title: __('用户名')},
                        {field: 'patient.name', title: __('就诊人')},
                        {field: 'servicebasic.name', title: __('Servicebasic.name')},
                        {field: 'order_sn', title: __('订单编号')},
                        {field: 'coupon_money', title: __('Coupon_money'), operate:'BETWEEN'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {
                                '1':'待支付','2':'已支付','3':'已匹配（待接单）','4':'已接单','5':'服务中','6':'已完成','7':'双方已评',
                                '10':'已取消','11':'支付超时自动取消','12':'失效加时单','15':'陪诊员取消订单申请中',
                                '41':'退款审核中','42':'退款成功','43':'退款拒绝'
                            }
                        },
                        {field: 'begin_time', title: __('begin_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'finish_time', title: __('Finish_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        appoint: function () {
            Controller.api.bindevent();
        },
        transfer: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});